/*
inputformat 为什么用他做参数 写这个函数的作用是什么 
onst [, ...values] = this.match(new RegExp(regexSource));
,.....value 什么意思  this。match是怎么匹配 
result[part] = values[index];  搞不明白  下来看录播都看不明白
*/

String.prototype.format = function(inputFormat,outputFormat){
    //1.提取每个时间部分的值
    const formatParts = [];//[YYYY,MM,DD,hh,mm,ss]
    //每匹配成功一次都会执行一次函数
    inputFormat = inputFormat.replace(/YYYY/,function(matched){
        formatParts.push(matched);
        return '(\\d{4})';
    });
    inputFormat = inputFormat.replace(/MM/,function(matched){
        formatParts.push(matched);
        return '(\\d{2})';
    });
    const regexp = new RegExp(regexpSource);
    //regexp.exec(str) str.match(regexp)
    //2023-01-22 03:04:05' (\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})
    const [,...values] = this.match(regexp);//values=[ '2023',01,22,03,04,05]
    //values=[2023,01,22,03,04,05]
    let parsedDate = {};
    formatParts.forEach((part,index)=>{
        parsedDate[part]=values[index]
    });
    console.log(formatParts);
    console.log(values);
    console.log(parsedDate);
    return outputFormat.replace(/YYYY|MM|DD|hh|mm|ss/g,function(matched){
        return parsedDate[matched];
    });
}

let dateStr = '2023-01-22 03:04:05';
let result = dateStr.format(
    'YYYY-MM-DD hh:mm:ss', //year month day hour minute second  输入的格式
    'YYYY年MM月DD日 hh时mm分ss秒'
);
console.log(result);